package com.gxuwz.com_property.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.com_property.business.dto.BusinessDto;
import com.gxuwz.com_property.business.pojo.Business;
import com.gxuwz.com_property.business.service.BusinessService;
import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.utils.RamdomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /**
     * 随机数
     * */
    @Autowired
    private RamdomUtils ramdomUtils;

    /**
     * 添加缴费信息
     */
    @PostMapping("/addBusinessInfo")
    public ResponseDto addBusinessInfo(@RequestBody Business business) {
        long business_id = ramdomUtils.generateId();
        business.setId(business_id);

        /**
         * 设置时间格式
         * */
        if (business.getPaymentDate() == null || business.getPaymentDate() == "") {
            LocalDateTime now = LocalDateTime.now(); // 获取当前时间
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 定义时间格式
            String mysqlDateTime = dtf.format(now); // 将LocalDateTime对象转换为符合MySQL的日期时间格式
            business.setPaymentDate(mysqlDateTime);
        } else {
            String originalDateTime = business.getPaymentDate();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.parse(originalDateTime), ZoneId.systemDefault());
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = dtf.format(localDateTime);
            business.setPaymentDate(mysqlDateTime);
        }

        Boolean result = businessService.addBusinessInfo(business);

        if (result) {
            return ResponseDto.success("缴费成功");
        } else {
            return ResponseDto.error("缴费失败");
        }
    }


    /**
     * 修改缴费信息
     */
    @PostMapping("/updateBusinessInfo")
    public ResponseDto updateBusinessInfo(@RequestBody Business business) {

        /**
         * 设置时间格式
         * */
        if (business.getPaymentDate()!=null){
            // 原始日期时间值
            String originalDateTime = business.getPaymentDate();
            // 将原始日期时间转换为 LocalDateTime 对象，并调整时区（这里假设您想要转换为系统默认时区）
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.parse(originalDateTime), ZoneId.systemDefault());
            // 将 LocalDateTime 对象转换为符合 MySQL 的日期时间格式
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = dtf.format(localDateTime);
            business.setPaymentDate(mysqlDateTime);
        }

        Boolean result = businessService.updateBusinessInfo(business);

        if (result) {
            return ResponseDto.success("修改缴费信息成功");
        } else {
            return ResponseDto.error("修改缴费信息失败");
        }
    }


    /**
     * 根据ID查询缴费信息
     */
    @PostMapping("/findBusinessByBusiness")
    public ResponseDto findBusinessByBusiness(Business business) {

        Business businessByBusiness = businessService.selectBusinessBybusiness(business);

        if (businessByBusiness != null) {
            return ResponseDto.success("查询缴费信息成功", businessByBusiness);
        } else {
            return ResponseDto.error("查询缴费信息失败", businessByBusiness);
        }
    }

    /**
     * 分页查询缴费信息
     */
    @PostMapping("/selectBusinessByPage")
    public ResponseDto selectBusinessByPage(@RequestBody BusinessDto businessDto) {
        Integer pageNo = businessDto.getPageNo();
        Integer pageSize = businessDto.getPageSize();

        IPage<BusinessDto> businessIPage = businessService.selectBusinessByPage(new Page<>(pageNo, pageSize), businessDto);

        if (businessIPage != null) {
            if (businessIPage.getTotal()>0){
                return ResponseDto.success("查询缴费信息成功", businessIPage);
            }else {
                return ResponseDto.error("没有该缴费信息", businessIPage);
            }

        } else {
            return ResponseDto.error("查询缴费信息失败", businessIPage);
        }
    }

    /**
     * 数据表格，查询最近一个月的数据
     * */
    @PostMapping("/getBusinessMangene")
    public ResponseDto BusinessChartMangene(@RequestBody BusinessDto businessDto) {

        List<Business> businesses = businessService.BusinessChartMangene(businessDto);

        if (businesses != null) {

            return ResponseDto.success("查询缴费信息成功", businesses);

        } else {
            return ResponseDto.error("查询缴费信息失败", businesses);
        }
    }
}
